<?php
namespace Home\Model;
use Common\Model\BaseModel;

class UserModel extends BaseModel {
    
    static $ROLE_WEIBAO  = 1;
    static $ROLE_MANAGER = 2;
    static $ROLE_SYS_MANAGER = 3;
    static $ROLE_COMPANY = 4;

    public $tableName = 'user';

    protected $_validate = array(
        array('name','require','登录名称不能为空!'),
        array('name','','登录名称已经存在！',0,'unique',1),
        array('password','require','密码能为空!'),
        array('repassword','password','确认密码不正确',0,'confirm'),
    );

    /**
     * @param $post
     * @return array
     * 更新或修改用户资料
     */
    public function saveUser($data){

        $ret = array('status'=>true,'msg'=>'');
        $post = $this->returnData($data);

        if(!$this->create($post)){

            $ret['status'] = false;
            $ret['msg'] = $this->getError();

        }else{

            unset($post['repassword']);
            $post['password'] = $this->encryptPass($post['password'],$post['name']);

            $res = (null !== $post['user_id']) ? $this->save($post) : $this->add($post);

            if(false !== $res){
                $ret['msg'] = '操作成功';
            }else{
                $ret['status'] = false;
                $ret['msg'] = $this->getDbError();
            }
        }

         return $ret;
    }

    public function returnData($post){

        $return = array();

        $return['user_id']      = !empty($post['user_id']) ? intval($post['user_id']) : null;
        $return['name']         = !empty($post['name']) ? addslashes($post['name']) : '';
        $return['nickname']     = !empty($post['nickname']) ? addslashes($post['nickname']) : '';
        $return['gender']       = !empty($post['gender']) ? intval($post['gender']) : 1;
        $return['borth']        = !empty($post['borth']) ? strtotime($post['borth']) : time();
        $return['work_time']    = !empty($post['work_time']) ? strtotime($post['work_time']) : time();
        $return['role_id']      = !empty($post['role_id']) ? intval($post['role_id']) : 1;
        $return['position_id']  = !empty($post['position_id']) ? intval($post['position_id']) : 1;
        $return['password']     = !empty($post['password']) ? addslashes($post['password']) : '';
        $return['repassword']   = !empty($post['repassword']) ? addslashes($post['repassword']) : '';
        $return['status']       = !empty($post['status']) ? intval($post['status']) : 1;
        $return['reg_time']     = !empty($post['reg_time']) ? strtotime($post['reg_time']) : time();
        $return['del']          = 1;

        return $return;
    }

    public function encryptPass($password,$token){

        $token = md5($token);
        $token = substr($token,0,5);
        return md5($password.$token);
    }

    public function getUserList($role){

        $list = $this->field('user_id,nickname,name')->where('role_id='.$role.' and status=1 and del=1')->select();
        
        return !empty($list) ? $list : array();
    }

    /**
     * @param $name
     * @return array
     * 通过用户登录名获取用户信息
     */
    public function getUserByName($name){

        $where = " name='{$name}' and status=1 and del=1";
        $info = $this->where($where)->select();

        return !empty($info) ? $info[0] : array();
    }
}